//
//  UIControl+GG.m
//  unzip
//
//  Created by yg on 2021/11/15.
//

#import "UIControl+GG.h"
#import <objc/runtime.h>

static const void *UIControlActionBlockDictionary = &UIControlActionBlockDictionary;

@implementation UIControl (GG)

- (void)gg_addActionBlock:(UIControlActionBlock)actionBlock {
	[self gg_addActionBlockForControlEvents:UIControlEventTouchUpInside withBlock:actionBlock];
}

- (void)gg_removeActionBlock{
	[self gg_removeActionBlocksForControlEvents:UIControlEventTouchUpInside];
}

///添加点击Block
- (void)gg_addActionBlockForControlEvents:(UIControlEvents)controlEvents withBlock:(UIControlActionBlock)actionBlock{
	[self gg_addActionBlockForActionId:nil controlEvents:controlEvents withBlock:actionBlock];
}

- (void)gg_addActionBlockForActionId:(NSString *)actionId controlEvents:(UIControlEvents)controlEvents withBlock:(UIControlActionBlock)actionBlock{
	
	NSMutableDictionary *actionBlocksDictionary = [self actionBlocksDictionary];
	
	UIControlActionBlockWrapper *blockActionWrapper = [[UIControlActionBlockWrapper alloc] init];
	blockActionWrapper.actionBlock = actionBlock;
	blockActionWrapper.controlEvents = controlEvents;
	
	NSString *key = actionId == nil ? [NSString stringWithFormat:@"events_%lu",(unsigned long)controlEvents] : actionId;
	
	//移除之前的事件
	UIControlActionBlockWrapper *lastBlockActionWrapper = actionBlocksDictionary[key];
	if (lastBlockActionWrapper) {
		[self gg_removeActionBlockForActionId:lastBlockActionWrapper.actionId];
	}
	
	[actionBlocksDictionary setObject:blockActionWrapper forKey:key];
	[self addTarget:blockActionWrapper action:@selector(invokeBlock:) forControlEvents:controlEvents];
}

	///根据ID移除点击事件
-  (void)gg_removeActionBlockForActionId:(NSString *)actionId{
	NSMutableDictionary *actionBlocksDictionary = [self actionBlocksDictionary];
	for (NSString *key in actionBlocksDictionary.allKeys) {
		UIControlActionBlockWrapper *wrapperTmp = actionBlocksDictionary[key];
		if ([actionId isEqualToString:key]) {
			[actionBlocksDictionary removeObjectForKey:key];
			[self removeTarget:wrapperTmp action:@selector(invokeBlock:) forControlEvents:wrapperTmp.controlEvents];
		}
	}
}
//移除点击Block
-  (void)gg_removeActionBlocksForControlEvents:(UIControlEvents)controlEvents{
	NSMutableDictionary *actionBlocksDictionary = [self actionBlocksDictionary];
	for (NSString *key in actionBlocksDictionary.allKeys) {
		UIControlActionBlockWrapper *wrapperTmp = actionBlocksDictionary[key];
		if (wrapperTmp.controlEvents == controlEvents) {
			[actionBlocksDictionary removeObjectForKey:key];
			[self removeTarget:wrapperTmp action:@selector(invokeBlock:) forControlEvents:controlEvents];
		}
	}
}


- (NSMutableDictionary *)actionBlocksDictionary {
	NSMutableDictionary *actionBlocksDictionary = objc_getAssociatedObject(self, UIControlActionBlockDictionary);
	if (!actionBlocksDictionary) {
		actionBlocksDictionary = [NSMutableDictionary dictionary];
		objc_setAssociatedObject(self, UIControlActionBlockDictionary, actionBlocksDictionary, OBJC_ASSOCIATION_RETAIN);
	}
	return actionBlocksDictionary;
}


@end

@implementation UIControlActionBlockWrapper

- (void)invokeBlock:(id)sender {
	if (self.actionBlock) {
		self.actionBlock(sender);
	}
}

@end
